package com.example.black.myapplication.paintlearn;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Typeface;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import com.example.black.myapplication.R;

/**
 * <pre>
 *     author : 28476
 *     e-mail : hao.wang@1hai.cn
 *     time   : 2018/12/26/15:29
 *     desc   :
 *     version: 1.0
 * </pre>
 */

public class CustomProgressBar extends View {
    public static final String TAG = CustomProgressBar.class.getSimpleName();
    private int max;
    private int roundColor;
    private int roundProgressColor;
    private int textColor;
    private float textSize;
    private float roundWidth;
    private boolean textShow;
    private int style;


    private  RectF oval;
    private int progress;
    private Paint paint;
    public static final int STROKE = 0;
    public static final int FILL = 1;
    private int center;
    private float radius;
    private float realPercent;
    private int percent;
    private float l,t,r,b;
    public CustomProgressBar(Context context) {
        super(context);
    }

    public CustomProgressBar(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        paint = new Paint();
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.CustomProgressBar);
        initAttr(typedArray);
        typedArray.recycle();
        initArgs();
    }

    public CustomProgressBar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        paint = new Paint();
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.CustomProgressBar);
        initAttr(typedArray);
        typedArray.recycle();
        initArgs();
    }

    private void initArgs() {
        oval = new RectF();
    }

    private void initAttr(TypedArray typedArray) {
        max = typedArray.getInteger(R.styleable.CustomProgressBar_max, 100);
        roundColor = typedArray.getColor(R.styleable.CustomProgressBar_roundColor, Color.RED);
        roundProgressColor = typedArray.getColor(R.styleable.CustomProgressBar_roundProgressColor, Color.BLUE);
        textColor = typedArray.getColor(R.styleable.CustomProgressBar_textColor, Color.GREEN);
        textSize = typedArray.getDimension(R.styleable.CustomProgressBar_textSize, 55);
        roundWidth = typedArray.getDimension(R.styleable.CustomProgressBar_roundWidth, 10);
        textShow = typedArray.getBoolean(R.styleable.CustomProgressBar_textShow, true);
        style = typedArray.getInt(R.styleable.CustomProgressBar_style, 0);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        center = w/2;
        radius = center - roundWidth/2;
        if (style == STROKE){
            l = center - radius;
            r = center + radius;
        }else{
            l = center - radius -roundWidth/2;
            r = center + radius +roundWidth/2;
        }
        t = l;
        b =  r;
        Log.d(TAG, "onSizeChanged");
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        realPercent = (progress/(float)max);
        percent = (int) (realPercent *100);
        paint.setColor(roundColor);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(roundWidth);
        paint.setAntiAlias(true);
        canvas.drawCircle(center, center, radius,paint);//使用相对坐标
        oval.set(l,t,r,b);
        paint.setColor(roundProgressColor);
        paint.setStrokeWidth(roundWidth);

        switch (style){
            case STROKE:
                paint.setStyle(Paint.Style.STROKE);//此view中，针对半径各填充一半的roundWidth；就实现画笔对应的效果
                canvas.drawArc(oval,0,360* realPercent,false,paint);
                break;
            case FILL:
                paint.setStyle(Paint.Style.FILL);
                if (progress != 0){
                    canvas.drawArc(oval , 0, 360 * realPercent, true, paint);
                }
                break;
        }
        paint.setColor(textColor);
        paint.setStrokeWidth(0);
        paint.setTextSize(textSize);
        paint.setTypeface(Typeface.DEFAULT_BOLD);
        if (textShow && percent != 0 ){
            Log.d(TAG, "onDraw paint.descent(): "+paint.descent());
            Log.d(TAG, "onDraw paint.ascent(): "+paint.ascent());
            canvas.drawText(
                    percent +"%"
                    ,(getWidth()-paint.measureText(percent +"%"))/2f
                    ,getWidth()/2f -( paint.descent() + paint.ascent())/2f//公式
                    ,paint
            );
        }
    }
//        @Override
//        protected void onDraw(Canvas canvas) {
//            super.onDraw(canvas);
//            //画默认的大圆环
//            int center = getWidth()/2;//中心坐标点
//
//            float radius =  center-roundWidth/2;//半径
//            paint.setColor(roundColor);
//            paint.setStyle(Paint.Style.STROKE);//设置空心(描边)
//            paint.setStrokeWidth(roundWidth);//圆环的宽度
//            paint.setAntiAlias(true);
//            canvas.drawCircle(center, center, radius , paint);
//
//            //画进度百分比
//        //		paint.reset();
//            paint.setColor(textColor);
//            paint.setStrokeWidth(0);//圆环的宽度
//            paint.setTextSize(textSize);
//            paint.setTypeface(Typeface.DEFAULT_BOLD);
//
//            int percent = (int) (progress/(float)max * 100);
//            if(textShow && percent!=0 && style == STROKE){
//                canvas.drawText(percent+"%", (getWidth()-paint.measureText(percent+"%"))/2f,
//                        //y公式： float baselineY = centerY + (fontMetrics.bottom-fontMetrics.top)/2 - fontMetrics.bottom
//                        getWidth()/2f-(paint.descent()+paint.ascent())/2f,
//                        paint);
//            }
//            //画圆弧
//            //矩形区域，定义圆弧的形状大小
//            RectF oval = new RectF(center-radius, center-radius, center+radius, center+radius);
//            paint.setColor(roundProgressColor);
//            paint.setStrokeWidth(roundWidth);
//            switch (style) {
//                case STROKE:
//                    paint.setStyle(Paint.Style.STROKE);
//                    canvas.drawArc(oval , 0, 360*progress/max, false, paint);
//                    break;
//                case FILL:
//                    paint.setStyle(Paint.Style.FILL_AND_STROKE);
//                    if(progress!=0)
//                        canvas.drawArc(oval , 0, 360*progress/max, true, paint);
//                    break;
//            }
//
//
//        }
    public synchronized int getMax() {
        return max;
    }

    public synchronized void setMax(int max) {
        if (max < 0){
            throw new RuntimeException("max must not be zero");
        }
        this.max = max;
    }

    public synchronized int getProgress() {
        return progress;
    }

    public synchronized void setProgress(int progress) {
        if (progress < 0){
            throw new IllegalArgumentException("progress 不能小于 0");
        }
        if(progress>max){
            progress = max;
        }
        if(progress <=max){
            this.progress = progress;
            postInvalidate();
        }
//        if(progress > max){
//            this.progress = max;
//
//        }else {
//            this.progress = progress;
//            postInvalidate();
//        }
    }

    public int getRoundColor() {
        return roundColor;
    }

    public void setRoundColor(int roundColor) {
        this.roundColor = roundColor;
    }

    public int getRoundProgressColor() {
        return roundProgressColor;
    }

    public void setRoundProgressColor(int roundProgressColor) {
        this.roundProgressColor = roundProgressColor;
    }

    public float getRoundWidth() {
        return roundWidth;
    }

    public void setRoundWidth(float roundWidth) {
        this.roundWidth = roundWidth;
    }

    public Paint getPaint() {
        return paint;
    }

    public void setPaint(Paint paint) {
        this.paint = paint; }

    public int getTextColor() {
        return textColor;
    }

    public void setTextColor(int textColor) {
        this.textColor = textColor;
    }

    public float getTextSize() {
        return textSize;
    }

    public void setTextSize(float textSize) {
        this.textSize = textSize;
    }

    public boolean isTextShow() {
        return textShow;
    }

    public void setTextShow(boolean textShow) {
        this.textShow = textShow;
    }

    public int getStyle() {
        return style;
    }

    public void setStyle(int style) {
        this.style = style;
    }
}
